// mypage/myCall/myCall.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    width: wx.getSystemInfoSync().windowWidth,
    num1: 0,
    num2: 0,
    num3: 0,
    num4: 0,
    scrollLeft: 0,
    nav_index: 0,
    com_data: [],
    show_address:true,
    text: '',
    txts: ["海量好机构等你来发掘","快去挑选宝贝喜欢的课程收藏吧","知识库海洋等你来遨游",'聚合名师，让孩子遇见喜欢的老师'],
    currentPage: 1,
    pageSize: 10,
    hasMoreData: true,
    token: '',
    userinfo: '',
    city: '',
    show_nav: false,
    educations:['小学','初中','高中','专科','本科','硕士','博士'],
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({token:wx.getStorageSync('token'),userinfo:wx.getStorageSync('userinfo'),city:wx.getStorageSync("city")})
    //
    that.call_all_count();
    that.get_call_cour_list();
  },
  // 导航
  check_nav:function(e){
    var that = this,nav_index=that.data.nav_index,txts=that.data.txts;
    var index = e.currentTarget.dataset.index;
    if(nav_index != index){
      that.setData({nav_index:index,currentPage:1,com_data:[],text:txts[index],show_nav:false})
      if(index==0){
        that.get_call_cour_list();
      }else if(index==1){
        that.get_call_mech_list();
      }else if(index==2){
        that.get_call_base_list();
      }else{
        that.get_call_teac_list();
      }
    }
    var curWidth = (that.data.width - 30) / 5;
    var moveWidth = (index - 1) * curWidth
    if (moveWidth > 0) {
      that.setData({ scrollLeft: moveWidth })
    } else {
      that.setData({ scrollLeft: 0 })
    }
  },
  // 跳转
  nav_home:function(){
    wx.switchTab({
      url: '../../pages/home/home',
    })
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,nav_index=that.data.nav_index;
    if(hasMoreData){
      if(nav_index==0){
        that.get_call_cour_list()
      }else if(nav_index==1){
        that.get_call_mech_list()
      }else if(nav_index==2){
        that.get_call_base_list()
      }else{
        that.get_call_teac_list()
      }
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 收藏总数
  call_all_count:function(){
    var that=this,token=that.data.token;
    my.call_all_count(token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({num1:info.courseCount,num2:info.orgCount,num3:info.repositoryCount})
      }
    })
  },
  // 收藏课程
  get_call_cour_list:function(){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,token=that.data.token,city=that.data.city;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "lat": city.lat,
      "lng": city.lng
    }
    my.get_call_cour_list(params,token).then(res=>{
      if(res.data.code==200){
        var list = res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].introducePictures=list[i].introducePictures?list[i].introducePictures.split(","):'';
          list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          list[i].labelName=list[i].labelName?list[i].labelName.split(","):''
          list[i].courseNatureName=list[i].courseNatureName?list[i].courseNatureName.substr(0,2):''
          list[i].sellCount=list[i].sellCount?list[i].sellCount:0
        }
        var com_data = that.data.com_data;
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
  // 收藏机构
  get_call_mech_list:function(){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,token=that.data.token,userinfo=that.data.userinfo,city=that.data.city;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "accountId": userinfo.id,
      "lat": city.lat,
      "lng": city.lng
    }
    my.get_call_mech_list(params,token).then(res=>{
      if(res.data.code==200){
        if(res.data.data){
          var list = res.data.data.records;
          for(var i=0;i<list.length;i++){
            list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
            list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          }
          var com_data = that.data.com_data;
          com_data = util.extend(com_data, list)
          if (list.length < pageSize) {
            that.setData({
              com_data: com_data,
              hasMoreData: false
            })
          } else {
            that.setData({
              com_data: com_data,
              hasMoreData: true,
              currentPage: currentPage + 1
            })
          }
          if(list.length==0&&com_data.length==0){
            that.setData({show_nav:true})
          }
        }else{
          that.setData({show_nav:true})
        }
      }
    })
  },
  // 收藏知识库
  get_call_base_list:function(){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,token=that.data.token,userinfo=that.data.userinfo;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "id": userinfo.id
    }
    my.get_call_base_list(params,token).then(res=>{
      if(res.data.code==200){
        var list = res.data.data.records;
        for(var i=0;i<list.length;i++){
          if(list[i]){
            list[i].viewCount=list[i].viewCount?(list[i].viewCount>=10000?parseFloat(list[i].viewCount/10000)+"万":list[i].viewCount):0
          }else{
            list.splice(i,1)
          }
        }
        var com_data = that.data.com_data;
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
  // 名师列表
  get_call_teac_list:function(){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,token=that.data.token,city=that.data.city,educations=that.data.educations;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "lat": city.lat,
      "lng": city.lng
    }
    my.get_call_teac_list(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.data;
        that.setData({num4:res.data.data.total})
        for(var i=0;i<list.length;i++){
          list[i].realName=list[i].realName?(list[i].realName.indexOf("老师")>-1?list[i].realName:list[i].realName+"老师"):'';
          list[i].yearCount=list[i].teachingTime?new Date().getFullYear()-new Date(list[i].teachingTime).getFullYear()+1:'';
          list[i].count=list[i].studentCount>10000?Math.ceil(list[i].studentCount/10000)+'万':list[i].studentCount;
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          list[i].educaName=list[i].education?educations[Number(list[i].education)-1]:'';
          list[i].positiveRate=list[i].positiveRate?list[i].positiveRate*100+'%':'';
        }
        var com_data = that.data.com_data;
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
})